package com.xuedongzhilian.controller;

import com.xuedongzhilian.dto.Result;
import com.xuedongzhilian.dto.StoryTaskDTO;
import com.xuedongzhilian.entity.StoryTask;
import com.xuedongzhilian.service.IStoryTaskService;
import com.xuedongzhilian.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 剧情任务控制器
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Slf4j
@RestController
@RequestMapping("/story-task")
public class StoryTaskController {

    @Resource
    private IStoryTaskService storyTaskService;

    /**
     * 获取用户剧情任务列表
     */
    @GetMapping
    public Result getUserStoryTasks(@RequestParam(required = false) Integer type) {
        Long userId = UserHolder.getUser().getId();
        List<StoryTask> storyTasks;
        
        if (type != null) {
            storyTasks = storyTaskService.lambdaQuery()
                    .eq(StoryTask::getUserId, userId)
                    .eq(StoryTask::getType, type)
                    .orderByDesc(StoryTask::getCreateTime)
                    .list();
        } else {
            storyTasks = storyTaskService.lambdaQuery()
                    .eq(StoryTask::getUserId, userId)
                    .orderByDesc(StoryTask::getCreateTime)
                    .list();
        }
        
        return Result.ok(storyTasks);
    }

    /**
     * 创建剧情任务
     */
    @PostMapping
    public Result createStoryTask(@RequestBody StoryTaskDTO storyTaskDTO) {
        StoryTask storyTask = new StoryTask();
        BeanUtils.copyProperties(storyTaskDTO, storyTask);
        storyTask.setUserId(UserHolder.getUser().getId());
        storyTask.setCreateTime(LocalDateTime.now());
        storyTask.setUpdateTime(LocalDateTime.now());
        
        boolean success = storyTaskService.save(storyTask);
        if (success) {
            return Result.ok(storyTask);
        } else {
            return Result.fail("创建任务失败");
        }
    }

    /**
     * 完成剧情任务
     */
    @PostMapping("/complete/{id}")
    public Result completeStoryTask(@PathVariable Long id) {
        Long userId = UserHolder.getUser().getId();
        StoryTask storyTask = storyTaskService.getById(id);
        
        if (storyTask == null) {
            return Result.fail("任务不存在");
        }
        
        if (!storyTask.getUserId().equals(userId)) {
            return Result.fail("无权操作该任务");
        }
        
        if (storyTask.getStatus() == 2) {
            return Result.fail("任务已完成");
        }
        
        // 更新任务状态
        storyTask.setStatus(2);
        storyTask.setCompleteTime(LocalDateTime.now());
        storyTask.setUpdateTime(LocalDateTime.now());
        
        boolean success = storyTaskService.updateById(storyTask);
        if (success) {
            // TODO 发放奖励（积分和道具）
            return Result.ok("任务完成，奖励已发放");
        } else {
            return Result.fail("完成任务失败");
        }
    }

    /**
     * 获取主线剧情任务
     */
    @GetMapping("/main")
    public Result getMainStoryTasks() {
        Long userId = UserHolder.getUser().getId();
        List<StoryTask> storyTasks = storyTaskService.lambdaQuery()
                .eq(StoryTask::getUserId, userId)
                .eq(StoryTask::getType, 1) // 主线任务
                .orderByDesc(StoryTask::getCreateTime)
                .list();
        return Result.ok(storyTasks);
    }

    /**
     * 获取支线任务任务
     */
    @GetMapping("/sub")
    public Result getSubStoryTasks() {
        Long userId = UserHolder.getUser().getId();
        List<StoryTask> storyTasks = storyTaskService.lambdaQuery()
                .eq(StoryTask::getUserId, userId)
                .eq(StoryTask::getType, 2) // 支线任务
                .orderByDesc(StoryTask::getCreateTime)
                .list();
        return Result.ok(storyTasks);
    }
}